/*
 *  Copyright 2010 s.tonouchi.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.tonouchi.staxframework;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * StaxFrameworkをユーザーが利用するときに使用するクラスです。
 * 基本的にこのクラスだけ使えばいいようにしたいです。
 *
 * @author s.tonouchi
 */
public class StaxFramework {

    private Configuration configuration = null;

    /**
     * このフレームワークを初期化します。
     *
     * @param configPath 設定ファイルのパス
     */
    public StaxFramework(String configPath) {
        try {
            configuration = new Configuration(configPath);
        } catch (Exception ex) {
        }
    }

    /**
     * インプットストリームが対象とするXMLを読み込みます。
     *
     * @param obj コマンドによって操作されるオブジェクト
     * @param is 入力ストリーム
     * @throws IOException IO例外
     * @throws XMLStreamException XML例外
     */
    public void read(Object obj, InputStream is) throws IOException, XMLStreamException {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(is);

        Stack<String> xmlStack = new Stack<String>();

        while (reader.hasNext()) {
            reader.next();
            switch (reader.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    xmlStack.push(reader.getLocalName());
                    Map<String, String> params = new HashMap<String, String>();

                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        params.put(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
                    }

                    StaxCommand command = CommandNameResolver.resolve(configuration, xmlStack);

                    command.execute(params, null, obj);
                    break;
                case XMLStreamConstants.CHARACTERS:
                    xmlStack.push("@CHARACTER");
                    command = CommandNameResolver.resolve(configuration, xmlStack);

                    command.execute(null, reader.getText(), obj);
                    xmlStack.pop();
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    xmlStack.pop();
                    break;
            }
        }
    }
}
